package customerEnter.Frame.Utils;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 讀取xml文件
 * 
 * 类名称：XMLFileUtil 类描述： 创建人：dy 创建时间：2016-6-17 下午4:01:00
 * 
 */
public class XMLFileUtil {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void test() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(
				"E:\\java\\apache-tomcat-8.0.33\\webapps\\yez_easyBuyMall\\WEB-INF\\classes\\UserLogin.xml"));
		Element root = document.getRootElement();
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element element = (Element) it.next();
			// 未知属性名称情况下
			Iterator attrIt = element.attributeIterator();
			while (attrIt.hasNext()) {
				Attribute a = (Attribute) attrIt.next();
				System.out.println(a.getValue().trim());
			}
			// 已知属性名称情况下
			// System.out.println("module: " +
			// element.attributeValue("module"));
			// 未知元素名情况下
			Iterator eleIt = element.elementIterator();

			while (eleIt.hasNext()) {
				Element e = (Element) eleIt.next();
				List<Attribute> list = e.attributes();
				for (Attribute string : list) {
					System.out.println(string.getName() + ":" + string.getValue());
				}
				System.out.println(e.getName().trim() + ": ");
				System.out.println(e.getText().trim());
			}
			// 已知元素名情况下
			/*
			 * System.out.println("title: " + element.elementText("title"));
			 * System.out.println("author: " + element.elementText("author"));
			 * System.out.println();
			 */
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void FindXML(String path) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(path));
		Element root = document.getRootElement();
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element element = (Element) it.next();
			// 未知属性名称情况下
			Iterator attrIt = element.attributeIterator();
			while (attrIt.hasNext()) {
				Attribute a = (Attribute) attrIt.next();
				System.out.println(a.getValue().trim());
			}
			// 已知属性名称情况下
			// System.out.println("module: " +
			// element.attributeValue("module"));
			// 未知元素名情况下
			Iterator eleIt = element.elementIterator();

			while (eleIt.hasNext()) {
				Element e = (Element) eleIt.next();
				List<Attribute> list = e.attributes();
				for (Attribute string : list) {
					System.out.println(string.getName() + ":" + string.getValue());
				}
				System.out.println(e.getName().trim() + ": ");
				System.out.println(e.getText().trim());
			}
			// 已知元素名情况下
			/*
			 * System.out.println("title: " + element.elementText("title"));
			 * System.out.println("author: " + element.elementText("author"));
			 * System.out.println();
			 */
		}
	}

	public static void main(String[] args) {
		try {
			test();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
